﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for acceptanceCriteriaPage.xaml
    /// </summary>
    public partial class acceptanceCriteriaPage : UserControl,IGenericPagesData
    {
        Button createNewAcceptanceCriteria;
        create createNewAcceptanceCriteriaItem;
        int userStoryId;
        public string navigationName = "";
        public acceptanceCriteriaPage(int userStoryId, string filter,string userStoryName)
        {
            InitializeComponent();
            this.userStoryId = userStoryId;
            navigationName = userStoryName + " Acceptance Criteria";
            refresh(filter);
        }
        public string getNavigationName()
        {
            return navigationName;
        }
        public int getId()
        {
            return userStoryId;
        }
        public void refresh(string filter)
        {
            lbAcceptanceCriteria.Items.Clear();
            
            try
            {

                DataClasses1DataContext db = new DataClasses1DataContext();
                var acceptanceCriteria = from p in db.tblAcceptanceCriterias where p.userStoryId == userStoryId select p;


                foreach (var p in acceptanceCriteria)
                {
                    if (filter == "")
                    {
                        acceptanceCriteriaItem theacceptanceCriteriaItem = new acceptanceCriteriaItem();
                        theacceptanceCriteriaItem.tbName.Text = p.name;
                        theacceptanceCriteriaItem.tbDesc.Text = p.description;
                        theacceptanceCriteriaItem.id = p.id;
                        theacceptanceCriteriaItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                        theacceptanceCriteriaItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);

                        lbAcceptanceCriteria.Items.Add(theacceptanceCriteriaItem);
                    }
                    else
                    {
                        if (p.name.IndexOf(filter) != -1)
                        {
                            acceptanceCriteriaItem theacceptanceCriteriaItem = new acceptanceCriteriaItem();
                            theacceptanceCriteriaItem.tbName.Text = p.name;
                            theacceptanceCriteriaItem.tbDesc.Text = p.description;
                            theacceptanceCriteriaItem.id = p.id;
                            theacceptanceCriteriaItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                            theacceptanceCriteriaItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);

                            lbAcceptanceCriteria.Items.Add(theacceptanceCriteriaItem);
                        }
                    }

                }
            }
            catch { }
            createNewAcceptanceCriteria = new Button { Content = "Create New Acceptance Criteria" };
            createNewAcceptanceCriteria.Click += new RoutedEventHandler(createNewAcceptanceCriteria_Click);
            createNewAcceptanceCriteria.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            lbAcceptanceCriteria.Items.Add(createNewAcceptanceCriteria);
        }



        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            acceptanceCriteriaItem theacceptanceCriteriaItem = new acceptanceCriteriaItem();
            theacceptanceCriteriaItem = (acceptanceCriteriaItem)helper.FindParrentUserControl((Button)sender, theacceptanceCriteriaItem.GetType().ToString());
            if (theacceptanceCriteriaItem.btnEdit.Content.ToString() == "Edit")
            {
                theacceptanceCriteriaItem.openTheDesc();
                theacceptanceCriteriaItem.btnEdit.Content = "Save";
            }
            else
            {
                if (theacceptanceCriteriaItem.tbName.Text == "")
                { MessageBox.Show("The Name Can't Be Empty"); }
                else
                {
                    theacceptanceCriteriaItem.closeTheDesc();
                    theacceptanceCriteriaItem.btnEdit.Content = "Edit";
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    tblAcceptanceCriteria tb = (from p in db.tblAcceptanceCriterias where p.id == theacceptanceCriteriaItem.id select p).First();
                    tb.name = theacceptanceCriteriaItem.tbName.Text;
                    tb.description = theacceptanceCriteriaItem.tbDesc.Text;
                    db.SubmitChanges();
                }
            }
        }

        void btnDell_Click(object sender, RoutedEventArgs e)
        {
            acceptanceCriteriaItem theacceptanceCriteriaItem = new acceptanceCriteriaItem();
            theacceptanceCriteriaItem = (acceptanceCriteriaItem)helper.FindParrentUserControl(((Button)sender), theacceptanceCriteriaItem.GetType().ToString());
            DataClasses1DataContext db = new DataClasses1DataContext();
            tblAcceptanceCriteria tb = (from p in db.tblAcceptanceCriterias where p.id == theacceptanceCriteriaItem.id select p).First();
            db.tblAcceptanceCriterias.DeleteOnSubmit(tb);
            db.SubmitChanges();

            lbAcceptanceCriteria.Items.Remove(theacceptanceCriteriaItem);
        }

        void createNewAcceptanceCriteria_Click(object sender, RoutedEventArgs e)
        {
            lbAcceptanceCriteria.Items.Remove(createNewAcceptanceCriteria);
            createNewAcceptanceCriteriaItem = new create();
            createNewAcceptanceCriteriaItem.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            createNewAcceptanceCriteriaItem.btnSave.Click += new RoutedEventHandler(btnSave_Click);

            lbAcceptanceCriteria.Items.Add(createNewAcceptanceCriteriaItem);
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (createNewAcceptanceCriteriaItem.tbName.Text != "")
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                tblAcceptanceCriteria tp = new tblAcceptanceCriteria { name = createNewAcceptanceCriteriaItem.tbName.Text, description = createNewAcceptanceCriteriaItem.tbDesc.Text, userStoryId = userStoryId  };
                db.tblAcceptanceCriterias.InsertOnSubmit(tp);
                db.SubmitChanges();

                refresh("");
            }
            else
            {
                MessageBox.Show("You need a name for your Acceptance Criteria");
            }
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            refresh("");
              
        }
    }
}
